package net.lab1024.sa.admin.module.business.bjd.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.OrderEntity;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.UserDollBindEntity;
import net.lab1024.sa.admin.module.business.bjd.domain.form.UserDollBindForm;
import net.lab1024.sa.admin.module.business.bjd.domain.form.UserDollBindQueryForm;
import net.lab1024.sa.admin.module.business.bjd.domain.vo.UserDollBindVo;
import net.lab1024.sa.admin.module.business.bjd.domain.vo.UserVo;
import net.lab1024.sa.admin.module.business.bjd.mapper.UserDollBindMapper;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @author guolong.zhang
 * @date 2025/02/09 09:36
 **/
@Service
@Slf4j
public class UserDollBindService {

    @Autowired
    private UserDollBindMapper userDollBindMapper;
    public PageResult<UserDollBindVo> queryPage(UserDollBindQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<UserDollBindVo> list = userDollBindMapper.pageList(page, queryForm);
        for (UserDollBindVo vo : list) {
            String orderNo = vo.getOrderNo();
            OrderEntity order = Db.lambdaQuery(OrderEntity.class)
                    .eq(OrderEntity::getOrderNo, orderNo)
                    .one();
            if(order != null){
                vo.setDollNo(order.getDollNo());
            }
        }
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public void unbind(UserDollBindForm req) {
        UserDollBindEntity bindEntity = Db.getById(req.getId(), UserDollBindEntity.class);
        if(null == bindEntity){
            throw new BusinessException("不存在绑定对象");
        }
        Db.removeById(bindEntity);
    }
}
